package org.ohos.rajawali3d.examples.examples.general;

import ohos.agp.render.opengl.EGLConfig;
import ohos.agp.render.opengl.GLES1X;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;
import org.ohos.rajawali3d.ResourceTable;
import org.ohos.rajawali3d.examples.examples.AExampleFragment;
import org.rajawali3d.Object3D;
import org.rajawali3d.debug.GLDebugger;
import org.rajawali3d.lights.DirectionalLight;
import org.rajawali3d.materials.Material;
import org.rajawali3d.materials.textures.ATexture;
import org.rajawali3d.materials.textures.Texture;
import org.rajawali3d.math.vector.Vector3;
import org.rajawali3d.primitives.Sphere;
import org.rajawali3d.renderer.Renderer;


public class DebugRendererFragment extends AExampleFragment {

    @Override
    public Renderer createRenderer() {
        final GLDebugger.Builder builder = new GLDebugger.Builder();
        builder.checkAllGLErrors();
        builder.checkSameThread();
        builder.enableLogArgumentNames();
        return new DebugRenderer(getContext(), builder);
    }

    private final class DebugRenderer extends org.rajawali3d.debug.DebugRenderer {

        private DirectionalLight mLight;
        private Object3D mSphere;

        public DebugRenderer(Context context, GLDebugger.Builder config) {
            super(context, config, false);
            setFrameRate(60);
        }

        @Override
        public void onRenderSurfaceCreated(EGLConfig config, GLES1X gl, int width, int height) {
            showLoader();
            super.onRenderSurfaceCreated(config, gl, width, height);
            hideLoader();
        }

        @Override
        public void onOffsetsChanged(float v, float v2, float v3, float v4, int i, int i2) {

        }

        @Override
        public void onTouchEvent(TouchEvent event) {

        }

        @Override
        protected void initScene() {
            mLight = new DirectionalLight(1.0, 0.2, -1.0); // set the direction
            mLight.setColor(1.0f, 1.0f, 1.0f);
            mLight.setPower(2);

            getCurrentScene().addLight(mLight);

            try {
                Material material = new Material();
                material.addTexture(new Texture("earthColors",
                        ResourceTable.Media_earthtruecolor_nasa_big));
                material.setColorInfluence(0);
                mSphere = new Sphere(1, 24, 24);
                mSphere.setMaterial(material);
                getCurrentScene().addChild(mSphere);
            } catch (ATexture.TextureException e) {
                e.printStackTrace();
            }

            getCurrentCamera().setLookAt(0, 0, 0);
            getCurrentCamera().setZ(6);
        }

        @Override
        protected void onRender(long elapsedRealtime, double deltaTime) {
            super.onRender(elapsedRealtime, deltaTime);
            mSphere.rotate(Vector3.Axis.Y, 1.0);
        }
    }
}
